// pages/couponBagCenter/couponBag.js
const controller=require("../controllers/Controller").controller

const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timeOver:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210511/841729101336150016.png",
    shopCoupon:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210511/841729285411569664.png",
    tradingAreaCoupon:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210511/841729358094663680.png",
    myList:[],
    otherList:[],
    showList:[],
    value:{
      shopId:"",
      couponType:1,
      state:0,
      paytype:0,
      types:6
    },
    selectIndex:0,
    select2Index:0
  },
  //跳转店铺首页
  toShopDetail(e){
    let shopId=e.currentTarget.dataset.shopid
    wx.reLaunch({
      url: '/pages/Index/Index?shopId='+shopId,
    })
  },

  //我购买和好友赠送切换
  changMyOrOther(e){
    let index=e.currentTarget.dataset.index
    this.setData({
      select2Index:index
    })
    this.changeList()
  },

  //切换list
  changeList(){
    if(this.data.select2Index===0){
      this.setData({
        showList:this.data.myList
      })
    }else{
      this.setData({
        showList:this.data.otherList
      })
    }
  },

  //跳转优惠券详情
  toCouponDetail(e){
    let state=e.currentTarget.dataset.state
    if(state==1){
      wx.showToast({
        icon:"none",
        title: '已使用',
      })
    }else if(state==2){
      wx.showToast({
        icon:"none",
        title: '已过期',
      })
    }else if(state==4){
      wx.showToast({
        icon:"none",
        title: '已赠送',
      })
    }else if(state===0){
      let id=e.currentTarget.dataset.id
      let couponAccountId=e.currentTarget.dataset.haveid
      let couponBagId=e.currentTarget.dataset.couponbagid
      // wx.requestSubscribeMessage({
      //   tmplIds:['DlEG_b9R6f2bWjrW-jEmsvhWwi5EwtF_vWG-pr3ZIds','DlEG_b9R6f2bWjrW-jEmsiB1UqeXvcEPjhoDA0azFXg'],
      //   success:res=>{
      //     console.log(res)
      //   },
      //   complete:()=>{
      //     wx.navigateTo({
      //       url: '/pages/couponBagCenter/couponDetail?id='+id+"&couponAccountId="+couponAccountId+'&couponBagId='+couponBagId,
      //     })
      //   }
      // })

      wx.navigateTo({
        url: '/pages/couponBagCenter/couponDetail?id='+id+"&couponAccountId="+couponAccountId+'&couponBagId='+couponBagId,
      })
    }
  },

  selectTap(e){
    let index=e.currentTarget.dataset.index
    this.setData({
      selectIndex:index
    })
    if(index===0){
      this.setData({
        'value.state':0,
        'value.couponState':""
      })
    }else if(index===1){
      this.setData({
        'value.state':1,
        'value.couponState':""
      })
    }else{
      this.setData({
        'value.state':1,
        'value.couponState':4
      })
    }
    this.getCouponList()
  },

  getCouponList(){
    let value=this.data.value
    value.shopId=wx.getStorageSync('shopId')
    controller.getCouponList(value).then(res=>{
      let myList=res.data.my
      let otherList=res.data.other
      myList.forEach(item=>{
       if(item.effectiveStartTime){
        item.effectiveStartTime=item.effectiveStartTime.split(" ")[0]
        item.effectiveEndTime=item.effectiveEndTime.split(" ")[0]
       }
      })
      otherList.forEach(item=>{
       if(item.effectiveStartTime){
        item.effectiveStartTime=item.effectiveStartTime.split(" ")[0]
        item.effectiveEndTime=item.effectiveEndTime.split(" ")[0]
       }
      })
        this.setData({
          myList:myList,
          otherList:otherList
        })
        this.changeList()
    })
  },

  //获取tab上的数目
  getNum(){
    let value1={}
    let value2={}
    let value3={}
    value1.shopId=wx.getStorageSync('shopId')
    value1.couponType=1
    value1.couponState=4
    value1.state=1
    value2.shopId=wx.getStorageSync('shopId')
    value2.couponType=1
    value2.state=0
    value3.shopId=wx.getStorageSync('shopId')
    value3.couponType=1
    value3.state=1
    controller.getCouponList(value1).then(res=>{
      this.setData({
        sendNum:res.data.my.length+res.data.other.length
      })
    })
    controller.getCouponList(value2).then(res=>{
      this.setData({
        noUseNum:res.data.my.length+res.data.other.length
      })
    })
    controller.getCouponList(value3).then(res=>{
      this.setData({
        historyNum:res.data.my.length+res.data.other.length
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getNum()
    this.getCouponList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  
  tabChange(e){
    var index=e.detail.index;
    if (index == 0) {
      wx.reLaunch({
         url: "/pages/Index/Index"
       });
     }
    if(index==1){
      // wx.requestSubscribeMessage({
      //   tmplIds:['CgSkX1UN123rlfAQ9zXiMyfiF1ugDkOsd8yLmvcbHt0','IoewIlKNaUfOLOhKUOdPKMOnJxdtdqWGfOotuOB2R70','TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'],//'TA4G0mjgPxvzTWGRTqvB9rkUfdot7EvAwJpRYfcLKcc'
      //   success:res=>{
      //     console.log(res)
      //   },
      //   complete:()=>{
      //     wx.reLaunch({
      //       url: "/pages/Mall/Mall"
      //     });
      //   }
      // })
      wx.reLaunch({
        url: "/pages/Mall/Mall"
      });
    }
    if(index==2){
      wx.reLaunch({
        url: "/pages/TradingArea/TradingArea"
      });
    }
    if(index==3){
      wx.reLaunch({
        url: "/pages/couponBagCenter/couponBagCenter"
      });
    }
    if(index==4){
      wx.reLaunch({
        url: "/pages/my/my-index/my-index"
      });
    }
  }
})